﻿using NUnit.Framework;
using Moq;
using AdventureWorks.DataAccess.repository;
using AdventureWorks.DataAccess.Task;
using AdventureWorks.DataAccess.SearchCriterias;
using NHibernate;
using AdventureWorks.Domain;
using System.Collections.Generic;
using System.Linq;
namespace AdventureWorks.DataAccess.Test.task
{
    [TestFixture]
    public class ProductInventoryTaskTest : TaskTestBase<IProductInventoryRepository, ProductInventoryRepository, ProductInventoryTask>
    {
        [Test]
        public void When_call_GetProductInventoryByLocation_should_call_repository()
        {
            ProductInventorySearchCriteria criteria = new ProductInventorySearchCriteria{ ProductNumber = "PN"};
            var result = task.GetProductInventoryByLocation(criteria);
            repositoryMock.Verify(x => x.GetProductInventory(sessionMock.Object, criteria));
        }

        [Test]
        public void When_call_GetProductInventoryTotal_should_call_repository()
        {
            ProductInventorySearchCriteria criteria = new ProductInventorySearchCriteria { ProductNumber = "PN" };
            var result = task.GetProductInventoryTotal(criteria);
            repositoryMock.Verify(x => x.GetProductInventory(sessionMock.Object, criteria));
        }

        [Test]
        public void When_call_GetProductInventoryTotal_should_return_sum_of_all_quantity()
        {
            ProductInventorySearchCriteria criteria = new ProductInventorySearchCriteria { ProductNumber = "PN" };
            repositoryMock.Setup(x => x.GetProductInventory(sessionMock.Object, criteria)).Returns(GetExamples().AsQueryable());
            var result = task.GetProductInventoryTotal(criteria);
            Assert.AreEqual(2, result.Count());
            var resultList = result.ToList();
            Assert.AreEqual(50, resultList.Where(x => x.Product.ProductNumber == "P1").Select(x => x.Quantity).FirstOrDefault());
            Assert.AreEqual(11, resultList.Where(x => x.Product.ProductNumber == "P2").Select(x => x.Quantity).FirstOrDefault());
        }

        private List<ProductInventory> GetExamples()
        {
            List<ProductInventory> list = new List<ProductInventory>
            {
                new ProductInventory{ Product =  new Product() { ProductNumber = "P1", Name = "PName1" , Id = 3},
                    Location = new Location { Name = "Location1", Id = 1},
                    Quantity = 23, Shelf = "Shelf1", Bin = 1},
                new ProductInventory{ Product =  new Product() { ProductNumber = "P1", Name = "PName1" , Id = 3},
                    Location = new Location { Name = "Location1", Id = 1},
                    Quantity = 11, Shelf = "Shelf2", Bin = 2},
                new ProductInventory{ Product =  new Product() { ProductNumber = "P1", Name = "PName1" , Id = 3},
                    Location = new Location { Name = "Location2", Id = 2},
                    Quantity = 16, Shelf = "Shelf2", Bin = 2},
                new ProductInventory{ Product =  new Product() { ProductNumber = "P2", Name = "PName2" , Id = 13},
                    Location = new Location { Name = "Location1", Id = 1},
                    Quantity = 4, Shelf = "Shelf1", Bin = 1},
                new ProductInventory{ Product =  new Product() {ProductNumber = "P2", Name = "PName2" , Id = 13},
                    Location = new Location { Name = "Location2", Id = 2},
                    Quantity = 7, Shelf = "Shelf2", Bin = 2},
            };
            return list;
        }
    }
}
